<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Config;

/**
 * 示例接口
 */
class Demo extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['test', 'test1','baidu','checkOcr','xcxNotice','synchronousData','diagnose'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['test2'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 测试方法
     *
     * @ApiTitle    (测试名称)
     * @ApiSummary  (测试描述信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/demo/test/id/{id}/name/{name})
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
     * @ApiParams   (name="name", type="string", required=true, description="用户名")
     * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
         'code':'1',
         'msg':'返回成功'
        })
     */
    public function test()
    {
        $this->success('返回成功', $this->request->param());
    }

    /**
     * 无需登录的接口
     *
     */
    public function test1()
    {
        $this->success('返回成功', ['action' => 'test1']);
    }

    /**
     * 需要登录的接口
     *
     */
    public function test2()
    {
        $this->success('返回成功', ['action' => 'test2']);
    }

    /**
     * 需要登录且需要验证有相应组的权限
     *
     */
    public function test3()
    {
        $this->success('返回成功', ['action' => 'test3']);
    }


    public function baidu()
    {
        $BaiduocrService = new \services\BaiduocrService();
        $url=input('url','htmlspecialchars');
        $res = $BaiduocrService->ocr($url);
        var_dump($res);exit;
    }


    public function checkOcr()
    {
        $play_order_no = ["635987478132","615923477867","685274040336","660973839965","689236039379","652860478637","671245478557","694310477320","678294474680","685436474669","602173474600","676851473679","690256473075","686910472764","687592477033","680734476582","634169470944"];
        foreach ($play_order_no as $key => $value) {
            $play_order_proof = Db::name('play_order_proof')->where(['play_order_no'=>$value])->order('id DESC')->find();
            $order = Db::name('play_order')->where(['play_order_no'=>$play_order_proof['play_order_no']])->find();
            $play = Db::name('play')->where(['play_no'=>$order['play_no']])->find();
            $OcrService = new \app\api\service\OcrService();
            $res = $OcrService->ocr1($play, $order, $play_order_proof ,$play_order_proof);
            echo '订单号：'.$play_order_proof['play_order_no'].'，阿里云识别结果：'.$play_order_proof['machine_audit_desc'].'，百度识别结果：'.json_encode($res['resarr'],JSON_UNESCAPED_UNICODE).'</br>';
        }
    }



    public function xcxNotice()
    {
        $openid="opWKY64mspRJ_YPaK7e4XD-RB62Q";
        $thing1="商品名称1";//商品名称
        $time3="2024-03-15 20:47:56";//完成时间
        $character_string5="45351465434312";//订单号
        $phrase10="订单状态";//订单状态
        $appid = Config::get('xcx.appid');
        $appSecret = Config::get('xcx.appsecret');
        $access_token = (new \app\api\service\BaseService())->xcxAccessToken($appid,$appSecret);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token;
        $data = [
            'thing1' => ['value'=>$thing1],
            'time3' => ['value'=>$time3],
            'character_string5' => ['value'=>$character_string5],
            'phrase10' => ['value'=>$phrase10],
        ];
        $postdata = [
            'access_token' => $access_token,
            'template_id' => 'tfjRgcG6kkzAbKgVdC4FYsW4rosrfDD_o9lKTHbnmRY',
            'page' => '/pages/index/order',
            'touser' => $openid,
            'data' => $data,
            'miniprogram_state' => 'formal',
        ];
        $params = json_encode($postdata);
        $header = array('Content-Type: application/json; charset=utf-8','Content-Length:' . strlen($params));
        $res = \fast\Http::post($url,$params,[],$header);
        return $res;
    }



    /**
     * 同步数据
     * @return [type] [description]
     */
    public function synchronousData()
    {
        $start_date = '2024-04-14 00:00:00';
        $end_date = '2024-04-17 00:00:00';
        $start_time = 1713024000;
        $end_time = 1713283200;
        $db2 = Db::connect(config('database.db2'));
        $db3 = Db::connect('mysql://root:root@127.0.0.1:3306/INFORMATION_SCHEMA#utf8');
        $tables = $db3->query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'chbc'");
        Db::startTrans();
        try {
            foreach ($tables as $key1 => $value1) {
                if ($value1['TABLE_NAME']=='chbc_user_token') continue;
                $slave_table = substr($value1['TABLE_NAME'], 5);
                $columns = $db3->query("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'chbc' and TABLE_NAME='".$value1['TABLE_NAME']."' and COLUMN_NAME ='createtime'");
                if (count($columns)) {
                    echo $value1['TABLE_NAME'].',';
                    $master_data = Db::query('select * from '.$value1['TABLE_NAME'].' where createtime>'.$start_time.' and createtime<'.$end_time);
                    $i=0;
                    foreach ($master_data as $key => $value) {
                        $slave_data = $db2->query('select id from '.$value1['TABLE_NAME'].' where id='.$value['id']);
                        if (!isset($slave_data[0]['id'])) {
                            $insert_data = $value;
                            $db2->name($slave_table)->insert($insert_data);
                            $i+=1;
                            // var_dump($db2->name($slave_table)->getLastSql());exit;
                        }
                    }
                    echo $i.'</br>';
                }
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            echo $e->getMessage();exit;
        }
        echo 'success';exit;
    }

}
